package com.scalar.chiptrack.operations.process.cycleTime.actioncommands;


import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.process.cycleTime.dao.StageCycleTimeDAO;
import com.scalar.chiptrack.operations.process.cycleTime.forms.StageCycleTimeForm;
import com.scalar.chiptrack.operations.process.VendorLocation;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;
/**
 * Created By Ganesh
 * Date  : Apr 3, 2006
 * Time  : 2:58:18 PM
 */
public class StageCycleTimeLoadPartNumbersCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/stageCycleTime.do";

    public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception
    {

        StageCycleTimeForm stageCycleTimeForm = ( StageCycleTimeForm ) form;

        HashMap errors                    = new HashMap();
        HashMap info                      = new HashMap();
        String action = request.getParameter ( "action" );

        request.setAttribute( INFO_MESSAGES, info );
        request.setAttribute( ERROR_MESSAGES, errors );


        HttpSession mSession              = request.getSession();
        User user                         = ( User ) mSession.getAttribute ( SessionKeys.USER_KEY );

        if ( user == null )
        {
            mSession.setAttribute ( SessionKeys.SOURCE_PAGE_KEY,  ACTION_NAME );
            return ( mapping.findForward ( "login" ) );
        }
        else
        {
            if ( user.isActionAllowed ( Functionality.STAGE_CYCLE_TIME_SETUP , User.USER_ACTION_MODIFY ) )
            {
                request.setAttribute ( "showMessage", "FALSE" );
            }
            else
            {
                request.setAttribute ( "showMessage", "TRUE" );
            }
        }

        try
        {
            ArrayList vendorLocationsList                       = StageCycleTimeDAO.loadAssemblyVendorLocations(null);
            ArrayList vendorLocationsLableValueBeans            = new ArrayList();
            int vendorLocationsListSize                         = vendorLocationsList.size();
            VendorLocation vendorLocation                       = null;
            ArrayList partNumberList                            = null;
            int locationId                                      = stageCycleTimeForm.getLocationId();

            stageCycleTimeForm.setProcessstepId(-1);
            stageCycleTimeForm.setStageDetails(null);


            if (vendorLocationsList != null)
            {
                for( int i = 0; i < vendorLocationsListSize; i++ )
                {
                    vendorLocation= (VendorLocation) vendorLocationsList.get(i);
                    vendorLocationsLableValueBeans.add ( new org.apache.struts.util.LabelValueBean( vendorLocation.getLocationName(), vendorLocation.getLocationID()+"") );
                }

            }
            partNumberList=new ArrayList();
            if( locationId != -1 )
            {
                partNumberList = StageCycleTimeDAO.loadAssemblyPartNumbers(null,locationId);
            }


            request.setAttribute ( "locationsList", vendorLocationsLableValueBeans );
            request.setAttribute ( "partNumberList", partNumberList );
            request.setAttribute ( "action", action );

        }
        catch( Exception e )
        {
            e.printStackTrace();
        }

        return mapping.findForward( "success" );
    }
}
